std::shared_ptr

From Cppreference

Jump to: navigation, search
Defined in header <memory>

template< class T > class shared_ptr;
(C++11 feature)

std::shared_ptr is a smart pointer that manages an object through a pointer to it. Several shared_ptr objects can link to the same object. It is guaranteed to be destroyed when the last shared_ptr pointing to it is destroyed or reset. The object is destroyed using delete expression or a custom deleter that is supplied along the pointer to the managed object during construction.

Contents

[edit] Member types

Member type Definition
element_type T

[edit] Member functions

(constructor)
constructs new shared_ptr
(public member function)
(destructor)
destructs the owned object if no more shared_ptrs link to it
(public member function)
operator=
assigns the shared_ptr
(public member function)
Modifiers
reset
replaces the managed object
(public member function)
swap
swaps the managed objects
(public member function)
Observers
get
returns a pointer to the managed object
(public member function)
operator*
operator->
dereferences pointer to the managed object
(public member function)
use_count
returns the number of shared_ptr objects referring to the same managed object
(public member function)
unique
checks whether the managed object is managed only by the current shared_ptr instance
(public member function)
operator bool
checks if there is associated managed object
(public member function)
owner_before
checks whether the managed object precedes managed object of another shared
pointer in implementation defined order, i.e. orders the managed objects
(public member function)

[edit] Non-member functions

std::swap(shared_ptr) (C++11)
specializes the std::swap() algorithm
(function template)
make_shared
creates a shared pointer that manages a new object
(function template)
allocate_shared
creates a shared pointer that manages a new object allocated using an allocator
(function template)
static_pointer_cast
dynamic_pointer_cost
const_pointer_cast
applies static_cast, dynamic_cast or const_cast to the type of the managed object
(function template)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox
In other languages